#!/usr/bin/perl
package TestCase;
use warnings;
use strict;
use Moose;
use Exporter qw(import);
use Utils;
use GlobalConfig;

# use Exporter qw(import);
# our @EXPORT=qw(atest_timestamp logfilename fail pass);

# use sigtrap 'handler' => \&end, 'normal-signals';

# use sigtrap 'handler' => \&signal_handler_kill, 'INT', 'ABRT', 'QUIT', 'TERM';

has this_run_log_file => (isa => 'Str', is => 'rw');
has ts_name => (  isa => 'Str', is => 'rw' );
has tc_name => (  isa => 'Str', is => 'rw' );
has chap =>    (  isa => 'Str', is => 'rw',default => '[0,0]' );
has status => (  isa => 'Str', is => 'rw',default => 'not_started' );
has is_critical => (  isa => 'Bool', is => 'rw',default => 0 );


# my $newest_instance;

sub prepare_check{
  my $self=shift;
  my $log_file=shift || $self->this_run_log_file;
#   $self->log("$config->{log_tag_start_tc}: ".$self->chap." ".$self->ts_name."|".$self->tc_name);
  $self->log(sprintf("%s: %s  %s::%s",$config->{log_tag_start_tc},
                      $self->chap,$self->ts_name,$self->tc_name));
  $self->log_test_profile();
  utils_warn "log file saved to $log_file" if ($log_file =~ m[^/tmp/]);
  $self->open_tc_log_monitor() if ($config->{open_tc_log_monitor});

}

sub create{
  my $self=shift;
  my $opt=shift;
  my $arg=shift;
  my ($package, $tc_name, $line) = caller;
  my $is_critical=$opt->{is_critical} || 0;
  my $log_file=$arg->[0] || "/tmp/$0.log";
  my $ts_name =$arg->[1] || $config->{log_tag_unknown_suite};
  my $chap    =$arg->[2] || "[unknown]";
#   print $ts_name."\n";

#prepare_check
#   $self->log("$config->{log_tag_start_tc}:".$ts_name."|".$tc_name, $log_file);
#   $self->log_test_profile($log_file);
#   warn "log file saved to $log_file" if ($log_file =~ m[^/tmp/]);
#   $self->open_tc_log_monitor($log_file) if ($config->{open_tc_log_monitor});
##end prepare_check  
  
  $self->new({this_run_log_file=>$log_file,tc_name=>$tc_name,
             is_critical=>$is_critical,ts_name=>$ts_name,chap=>$chap});
}

sub BUILD{
  my $self=shift;
#   $newest_instance=$self;
  $self->prepare_check;
}

sub log{
  my $self=shift;
#   my($self,$txt)=@_;
  my $txt=shift || "";
  my $log_file=shift || $self->this_run_log_file;
  $txt= "$config->{color_log_prompt}".$self->ts_name."|".$self->tc_name."|".utils_timestamp."$config->{color_reset}: "."$txt";
#   print "logging to $log_file: $txt\n";
  utils_log($txt,$log_file);
}

sub open_tc_log_monitor{
  my $self=shift;
  my $log_file=shift || $self->this_run_log_file;
  my $xterm_cmd="xterm -title $log_file -e 'touch $log_file;tail -c+1 --retry -F $log_file'&";
#   print $xterm_cmd."\n";
  utils_run_cmd($xterm_cmd);
}

sub log_test_profile{
  my $self=shift;
  my $log_file=shift|| $self->this_run_log_file;
  my ($tc_name,$is_critical,$ts_name)=($self->tc_name,$self->is_critical,$self->ts_name);
  my $profile_prefix="$config->{color_log_profile_prompt}$ts_name|$tc_name|TC_PROFILE=>$config->{color_reset}";
  my $profile_txt=<<EOF;
  
$profile_prefix log_file :: $log_file
$profile_prefix is_critical :: $is_critical
EOF
  utils_log($profile_txt,$log_file);
}



sub close_tc_log_monitor{
  my $self=shift;
  my $log_file=$self->this_run_log_file;
  my $close_xterm_cmd="bash -c 'sleep $config->{tc_log_monitor_retain_time};ps -ef |grep $log_file |grep xterm |grep -v grep| awk \"{print \\\$2}\" |xargs -i kill {}'&";
#   print $close_xterm_cmd."\n";
  utils_run_cmd($close_xterm_cmd);
}

sub signal_handler_kill{
#   if ($newest_instance) {
#     $newest_instance->status("timeout");
#     $newest_instance->end;
#   }
}

sub end{
  my $self=shift;
  my $self_status=$self->status;
  my %tc_status=%{$config->{tc_status}};
  $self->status("unknown") if (!exists $tc_status{$self_status});

###TODO
  $self->status("passed") if ($self->status !~ m/^(failed|timeout|error)$/ && $self->status eq "not_started");
###
  my $color_status="$config->{color_log_pass}".$self->status."$config->{color_reset}";
  
  $color_status="$config->{color_log_fail}".$self->status."$config->{color_reset}" if ($self->status ne "passed");
  
  $self->log(sprintf("%s %s: %s %s",
            $self->tc_name,$config->{log_tag_tc_status},$self->chap,$color_status));
  $self->close_tc_log_monitor() if ($config->{open_tc_log_monitor});

#   $self->log("$config->{log_tag_end_tc}: ".$self->chap." ".$self->ts_name."|".$self->tc_name);
#   
  $self->log(sprintf("%s: %s  %s::%s",$config->{log_tag_end_tc},
                      $self->chap,$self->ts_name,$self->tc_name));
#   utils_alert($self->name." \"".$self->status."\n log file: ". $self->this_run_log_file."\"" ) if ($self->status ne "passed");
  utils_alert($self->tc_name." ".$self->status) if ($self->status ne "passed");
}

# sub new{
# #   my $self=shift;
# #   my $log_file=$self->this_run_log_file();
# #   warn "log file saved to $log_file" if ($log_file =~ m[/tmp/]);
# }

# sub say_name {
#        my $self = shift;
#         say "The name given to me is ".$self->name.".\n";
#   }

# sub fail {
# 1;
# }
# 
# sub pass {
# 1;
# }

1;